package com.bidevalution.business.job;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeComputerResult;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.context.Constants;
import com.google.common.collect.Maps;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 资格审查-资质、资格、许可证检查（无业绩）
 * @author: xiayf
 * @date: 2019-11-23 21:30
 */
public class EnterpriseQualificationCheckJob implements StepJobJdbcFactory {

    private JdbcTemplateService jdbcTemplateService;
    private Logger log = LogManager.getLogger(getClass());
    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }

    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId, String layouCode) throws Exception {
        try {
            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("sectionId", beSectionStepJob.getSectionId());
            paramMap.put("sectionStepId",beSectionStepJob.getSectionStepId());
            String sqlStr = " SELECT " +
                    "a.seq_id enterpriseId, " +
                    "COUNT(d.Guid) errorCount " +
                    "FROM be_section_enterprise a " +
                    "LEFT JOIN be_section_info sec ON a.section_id=sec.seq_id " +
                    "LEFT JOIN td_bidinfo b ON a.seq_id = b.enterprise_id and b.biin_projectId=sec.td_projectId " +
                    "LEFT JOIN be_enterprise_check c ON c.orgCode=a.credit_code " +
                    "LEFT JOIN be_enterprise_check_item d ON c.EnterpriseGUID=d.EnterPersonGuid AND d.CheckResultViw <> '正常' " +
                    "where a.section_id=:sectionId " +
                    "GROUP BY a.seq_id ";
            List<Map<String, Object>> enterList = jdbcTemplateService.selectForList(sqlStr, paramMap);
            //保存计算结果
            saveResult(paramMap, enterList,beSectionStepJob);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new Exception("*****资格审查-资质、资格、许可证检查失败*****");
        }
    }

    private void saveResult(Map<String, Object> paramMap, List<Map<String, Object>> enterList,BeSectionStepJob beSectionStepJob) throws Exception {
        try {
            List<BeComputerResult> beComputerResultList = new ArrayList<>();
            BeComputerResult beComputerResult = null;
            for (Map<String, Object> dataMap : enterList) {
                beComputerResult = new BeComputerResult();
                beComputerResult.setSeqId(IdsUtil.uuid());
                beComputerResult.setSectionStepId(beSectionStepJob.getSectionStepId());
                beComputerResult.setSectionId(beSectionStepJob.getSectionId());
                beComputerResult.setEnterpriseId(OConvertUtils.getString(dataMap.get("enterpriseId")));
                //错误数
                beComputerResult.setC1(OConvertUtils.getString(dataMap.get("errorCount")));
                //是否通过
                if (OConvertUtils.getInt(dataMap.get("errorCount")) > Constants.ZERO) {
                    beComputerResult.setIsPassed(Constants.ZERO.toString());
                } else {
                    beComputerResult.setIsPassed(Constants.ONE.toString());
                }
                beComputerResult.setCreateTime(DateUtil.now());
                beComputerResultList.add(beComputerResult);
            }
            SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(beComputerResultList);
            jdbcTemplateService.update(" DELETE FROM be_computer_result WHERE section_id=:sectionId AND section_step_id =:sectionStepId ", paramMap);
            jdbcTemplateService.bathInsert(" INSERT INTO be_computer_result(seq_id,section_step_id,section_id,enterprise_id,c1,is_passed,create_time) " +
                    " VALUES(:seqId, :sectionStepId, :sectionId,:enterpriseId,:c1,:isPassed,:createTime) ", beanSources);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }
}
